<title>所有渠道代理商</title>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>渠道代理商管理</cite></a>
    <a><cite>所有渠道代理商</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <!--<div class="layui-tab layui-tab-brief">-->
    <!--<ul class="layui-tab-title">-->
    <!--<li class="layui-this">渠道商列表</li>-->
    <!--</ul>-->

    <div class="layui-tab-content">

      <div class="layui-btn-group demoTable lay-mb15">
        <!--<div class="layui-form" style="float:right;">-->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
          <div class="layui-form-item" style="margin:0;">
            <!--<div class="layui-input-inline">-->
            <!--<label class="layui-form-label">商户ID</label>-->
            <!--<div class="layui-input-block">-->
            <!--<input type="text" name="mchId" id="mchId" placeholder="请输入商户ID" autocomplete="off" class="layui-input">-->
            <!--</div>-->
            <!--</div>-->
            <div class="layui-inline">

              <label class="layui-form-label">渠道代理ID</label>
              <div class="layui-input-block">
                <input type="text" name="channelAgentMchId" id="channelMchId" placeholder="请输入渠道代理商ID" autocomplete="off" class="layui-input">
              </div>
            </div>

            <div class="layui-inline">
              <!--<button id="search" class="layui-btn  layuiadmin-btn-admin" data-type="reload"><i class="layui-icon layui-icon-search"></i></button>-->
              <button id="search" class="layui-btn layuiadmin-btn-admin" data-type="reload">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
              </button>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button id="addChannelAgentMch" class="layui-btn layui-btn-normal" data-type="add"><i class="layui-icon"></i>添加
          </button>
          <button id= "output" class="layui-btn layui-btn-warm" data-type="output"><i class="layui-icon layui-icon-download-circle"></i>导出
          </button>
          <button id= "batchdel" class="layui-btn layui-btn-danger" data-type="batchdel"><i class="layui-icon"></i>删除
          </button>
        </div>

        <!--<div class="layui-row">-->
        <!--<div class="layui-form-item">-->
        <!--<blockquote class="layui-elem-quote" id="amountTip"></blockquote>-->
        <!--</div>-->
        <!--</div>-->
        <table id="ChannelAgentMch_dataAll" lay-filter="ChannelAgentMch_dataAll"></table>
      </div>
    </div>
  </div>

</div>
<style>
  .viewInfo {
    padding: 30px;
    font-size: 14px;
    color: #666;
  }

  .viewInfo li {
    margin-bottom: 15px;
  }

  .viewInfo li span {
    display: inline-block;
    width: 25%;
    text-align: right;
    margin-right: 30px;
    font-weight: bold;
  }


</style>
<script>
    layui.use(['admin', 'table', 'util'], function(){
//
        var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,element = layui.element
            ,form = layui.form;

        element.render('breadcrumb', 'breadcrumb');

        var tplStatus = function(d){
            if(d.status == 1) {
                //  alert(1);
//          return '<span class="layui-badge layui-bg-green">启用</span>';
                return '  <input type="checkbox" name="status" lay-filter="layTableSwitchForChanelAgentMch" lay-skin="switch" lay-text="开|关" checked="true"> <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>开</em> <i></i></div>';
//          return '<input type="checkbox" name="status" lay-skin="switch" lay-text="是|否" checked="'+d.status+'">';
            }

            return ' <input type="checkbox" name="status" lay-filter="layTableSwitchForChanelAgentMch" lay-skin="switch" lay-text="开|关" > <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>关</em><i></i></div>';

        };
        var tplLevel = function(d){
            if(d.channelAgentMchLevel == 1) {
               return '<span class="layui-badge layui-bg-green">高级渠道代理商</span>';
            }else if(d.channelAgentMchLevel == 2){
                return '<span class="layui-badge layui-bg-green">中级渠道代理商</span>';
            }else{
                return '<span class="layui-badge layui-bg-green">普通渠道代理商</span>';
            }


        };
        var tplEdit = function(d){
            return "<a  href="+ d.loginMchUrl +" class='layui-btn layui-btn-small layui-btn-xs' target='_blank'>登录系统</a>" +
                "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='merchantDetail'>查看信息</a>" +
                "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='settConfig'>结算设置</a>" +
                "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='payPassage'>通道归属</a>" +
                "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='agentpayPassage'>代付通道</a>" +
                "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='change'>余额变更</a>" +
                    /*"<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='pwdReset'>重置密码</a>" +*/
                "<a class='layui-btn layui-btn-xs' lay-event='edit'>编辑</a>";
        };
        var tplAccount = function(d){
            return  "<div style='background-color: #5FB878'>" +
                "<span style='color: gold'>账户余额:" + d.balance/100 +"元 &nbsp &nbsp</span>" +
                "<span style='color: white'>代付余额:" + d.agentpayBalance/100 +"元 &nbsp &nbsp</span>" +
                "<span style='color: red'>冻结金额:" + d.frozenMoney/100 +"元 &nbsp &nbsp</span>" +
                "<span style='color: brown'>保证金额:" + d.securityMoney/100 +"元 </span>" +
                "</div>"
        };

//      admin.req({
//          type: 'get',
//          url: layui.setter.baseUrl + '/account/get?mchId=' + mchId,
//          error: function(err){
//              layer.alert(err.msg,{title:'请求失败'})
//          },
//          success:function(res){
//              if(res.code == 0 && res.data != null){
//                 // $('#mchId').val(res.data.mchId);
//                  $('#amountTip').html(
//                      "账户余额: <span style='color: green'>" + res.data.balance / 100 + "</span> 元 " +
//                      "代付余额: <span style='color: green'>" + res.data.agentpayBalance / 100 + "</span> 元 " +
//                      "冻结金额: <span style='color: red'>" + res.data.frozenMoney / 100 + "</span> 元 " +
//                      "保证金额: <span style='color: red'>" + res.data.securityMoney / 100 + "</span> 元"
//                  );
//              }
//          }
//      });

        //商户列表
        table.render({
            elem: '#ChannelAgentMch_dataAll'
            ,url: layui.setter.baseUrl + '/channelagentmch_info/list' //列表接口
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id: 'tableReload'
            ,page: true
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'channelAgentMchId', fixed: 'left',title: '渠道代理商ID', width: 120}
                ,{field: 'userName', title: '登录账户', width: 120}
                ,{field: 'channelAgentMchName', title: '渠道代理商名称', width: 130}
                ,{field: 'supChannelAgentMchAgentId', title: '上级渠道代理商ID', width: 150}
                ,{field: 'channelAgentMchLevel', title: '渠道代理商等级', width: 130,templet: tplLevel}
                ,{field: 'status', title: '状态', width: 80,templet: tplStatus}
                ,{field: 'account', title: '账户总额', width: 600, templet: tplAccount}
            /*,{field: 'createTime', title: '时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}*/
                ,{field: 'edit', title: '操作', width: 500, templet: tplEdit }
            ]]
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(ChannelAgentMch_dataAll)', function(obj){
            var data = obj.data;
            if(obj.event === 'merchantDetail'){
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/channelmch_info/get',
                    data: {
                        channelMchId : data.channelMchId
                    },
                    error: function(err){
                        layer.alert(JSON.stringify(err.field), {
                            title: '系统异常'
                        })
                    },
                    // layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
                    success: function(res){
                        if(res.code == 0){
                            layer.open({
                                title: '查看渠道信息',
                                type: 1,
                                //skin: 'layui-layer-rim',
                                shadeClose: true,
                                area: admin.screen() < 2 ? ['80%', '300px'] : ['500px', '350px'],
                                content: '<div id="view">'
                                +'<ul class="viewInfo">'
                                +'<li><span>渠道商ID：</span>'
                                + res.data.channelMchId
                                + '</li>'
                                +'<li><span>上级代理渠道ID：</span>'
                                + res.data.supChannelMchAgentId
                                + '</li>'
                                +'<li><span>渠道商名称：</span>'
                                + res.data.channelMchName
                                + '</li>'
                                +'<li><span>登录账户：</span>'
                                + res.data.userName
                                + '</li>'
                                +'<li><span>联系方式：</span>'
                                + res.data.mobile
                                + '</li>'
                                +'<li><span>邮箱：</span>'
                                + res.data.email
                                + '</li>'
                                +'<li><span>qq：</span>'
                                + res.data.qq
                                + '</li>'
                                +'</ul>'
                                +'</div>'
                            });
                        }
                    }
                });
                //  location.href = layui.setter.baseLocal + "merchant/list/mch_view/mchId=" + data.mchId;
            } else if(obj.event === 'accountDetail'){
                location.href = layui.setter.baseLocal + "merchant/list/account_view/mchId=" + data.mchId;
            } else if(obj.event === 'payPassage'){
//        if(data.type == 2) {
//          layer.msg('私有账户,无需设置!');
//          return;
//        }
//        location.href = layui.setter.baseLocal + "merchant/pay_passage/list/mchId=" + data.mchId;
                layer.open({
                    title: '分配渠道商通道',
                    type: 2,
                    //skin: 'layui-layer-rim',
                    shadeClose: false,
                    area: admin.screen() < 2 ? ['80%', '300px'] : ['800px', '600px'],
                    content: layui.setter.base + 'views/channelmch/list/channel_add.html'
//              content: layui.setter.baseUrl + '/channelmch_passage/getPayPassageList',
                });

            } else if(obj.event === 'agentpayPassage'){
                if(data.type == 2) {
                    layer.msg('私有账户,无需设置!');
                    return;
                }
                location.href = layui.setter.baseLocal + "merchant/agentpay_passage/list/mchId=" + data.mchId;
            } else if(obj.event === 'settConfig'){
                if(data.type == 2) {
                    layer.msg('私有账户,无需设置!');
                    return;
                }
                location.href = layui.setter.baseLocal + "merchant/list/mch_sett/mchId=" + data.mchId;
            }else if(obj.event === 'pwdReset'){
                layer.prompt({
                    formType: 1,
                    title: '请输入新密码'
                }, function(value, index, elem){
                    admin.req({
                        type: 'get',
                        url: layui.setter.baseUrl + '/mch_info/pwd_reset',
                        data: {
                            mchId : data.mchId,
                            password : value
                        },
                        error: function(err){
                            layer.alert(JSON.stringify(err.field), {
                                title: '错误提示'
                            })
                        },
                        success: function(res){
                            if(res.code == 0){
                                layer.msg('修改成功', {icon: 1});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === 'change'){
                location.href = layui.setter.baseLocal + "merchant/list/mch_change/mchId=" + data.mchId;
            }else if(obj.event === 'edit'){
                //    http://localhost:8193/x_mgr/src/views/merchant/list/mch_edit.html?v=1.0.0
                //  /x_mgr/start/index.html#/merchant/list/mch_edit/mchId=20000006
                // '/x_mgr/start/index.html#/'
                //  alert(layui.setter.baseLocal + "merchant/list/mch_edit/mchId=" + data.mchId);
//          layer.open({
//              title: '新增',
//              type: 2,
//              skin: 'layui-layer-rim',
//              shadeClose: false,
//              area: admin.screen() < 2 ? ['80% ', '300px'] : ['800px', '600px'],
//              content: layui.setter.baseUrl + "merchant/list/mch_edit.html/mchId=" + data.mchId
//
////                    content: '<div id="addContent"></div>'
//          });
                location.href = layui.setter.baseLocal + "merchant/list/mch_edit/mchId=" + data.mchId;
            }

        });

        form.on("switch(layTableSwitchForChanelAgentMch)",function(data){
            //  layer.msg(this.checked ? "true" : "false");

            var index_sms;
            // 获取当前控件
            var selectIfKey=data.othis;
            // 获取当前所在行
            var parentTr = selectIfKey.parents("tr");
            // 获取当前所在行的索引
            var parentTrIndex = parentTr.attr("data-index");
            var channelAgentMchId=parentTr.find(('td:eq(1)')).text().trim();
           // alert(chanchannelAgentMchId);
            admin.req({
                type: 'get',
                url: layui.setter.baseUrl + '/channelagentmch_info/updateStatus',
                data: {
                    channelAgentMchId : channelAgentMchId,
                    status : this.checked ? "true" : "false"
                },
                beforeSend:function(){
                    index_sms = layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
                },
                error: function(err){
                    layer.alert(JSON.stringify(err.field), {
                        title: '错误提示'
                    })
                },
                success: function(res){
                    if(res.code == 0){
                        layer.close(index_sms);
                        layer.msg('修改渠道代理商状态成功', {icon: 6});
                    }
                }
            });
            //       layer.msg(this.checked ? "true" : "false");

            // layer.msg( data.channelMchId);
        })
        // 搜索
//    var $ = layui.$, active = {
//      reload: function(){
//          alert(1);
//        var channelMchId = $('#channelMchId').val();
//        //执行重载
//        table.reload('tableReload', {
//          page: {
//            curr: 1 //重新从第 1 页开始
//          }
//          ,where: {
//                channelMchId: channelMchId
//          }
//        });
//      }
//    };
        $('#search').on('click', function() {
            var channelMchId = $('#channelMchId').val();
            alert(channelMchId);
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('#addChannelAgentMch').on('click', function() {

            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('#batchdel').on('click', function() {

            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var active = {
            reload: function(){
                var channelMchId = $('#channelMchId').val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        channelMchId: channelMchId
                    }
                });
            },
            batchdel: function(){
                var checkStatus = table.checkStatus('tableReload')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除选中的渠道代理商吗？', function(){
                    var channelAgentMchIds = "";
                    $.each(checkData, function(i, n){
                        channelAgentMchIds = channelAgentMchIds + "," + n.channelAgentMchId;
                    });
                    if(channelAgentMchIds != "") {
                        channelAgentMchIds = channelAgentMchIds.substring(1);
                    }
                    admin.req({
                        type: 'get',
                        url: layui.setter.baseUrl + '/channelagentmch_info/delete',
                        data: {
                            channelAgentMchIds: channelAgentMchIds,
                        },
                        success: function(res){
                            if(res.code == 0) {
                                layer.msg('删除成功', {
                                    icon: 1
                                });
                                table.reload("tableReload");
                            }
                        }
                    });
                });
            },
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('tableReload')
                    , data = checkStatus.data;
                active.getId(data)

                layer.alert(JSON.stringify(data));
            }
            , getId: function (data) {
                var idArr = [];
                for (var i = 0; i < data.length; i++) {
                    console.log(data[i].id)
                    idArr.push(data[i].id)
                }
                console.log(idArr)
            }
            , getCheckLength: function () { //获取选中数目
                var checkStatus = table.checkStatus('tableReload')
                    , data = checkStatus.data;
                layer.msg('选中了：' + data.length + ' 个');
            }
            , isAll: function () { //验证是否全选
                var checkStatus = table.checkStatus('tableReload');
                layer.msg(checkStatus.isAll ? '全选' : '未全选')
            }
            , add: function() { // 增加一条列表
                layer.open({
                    title: '新增渠道代理商',
                    type: 2,
                    //skin: 'layui-layer-rim',
                    shadeClose: false,
                    area: admin.screen() < 2 ? ['80%', '300px'] : ['800px', '600px'],
                    content: layui.setter.base + 'views/channelagentmch/list/channelagentmch_add.html'
                });
            }
        };


        // 渲染表单
        form.render();
    });
</script>